package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerStall;
import com.ian.service.MerchantAuthService;
import com.ian.service.PerStallService;
import com.ian.service.PerUserService;
import com.ian.vo.StallInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 车位控制层
 */
@Api("车位相关API")
@RestController
public class PerStallController {
    @Autowired
    PerStallService perStallService;

    @Autowired
    PerUserService perUserService;

    @Autowired
    MerchantAuthService merchantAuthService;

    @ApiOperation("添加车位")
    @PostMapping(value = "/insertStall", produces = "application/json;charset=UTF-8")
    public ResultMsg insertStall(@RequestBody StallInfo stallInfo) {
        if (stallInfo.getMobile() != null) {
            PerStall perStall = new PerStall();
            perStall.setPerUserId(perUserService.findByTel(stallInfo.getMobile()).getPerUserId());
            perStall.setPerStallAddress(stallInfo.getAddress());
            perStall.setPerStallLnglat(stallInfo.getLnglat());
            perStall.setPerStallNum(stallInfo.getNum());
            perStall.setPerStallStarttime(stallInfo.getStartTime());
            perStall.setPerStallEndtime(stallInfo.getEndTime());
            perStall.setPerStallAmount(stallInfo.getAmount());
            perStall.setPerStallPrice(stallInfo.getPrice());
            perStall.setPerStallStatus(0);
            perStallService.insert(perStall);
            return ResultMsg.success("添加车位成功~", stallInfo.getMobile());
        }
        return ResultMsg.failed("添加车位失败~");
    }

    @ApiOperation("查询经纬度")
    @GetMapping(value = "/points", produces = "application/json;charset=UTF-8")
    public ResultMsg selectPoint() {
        List list = new ArrayList();
        for (String p1 : perStallService.selectPoint()) {
            list.add(p1);
        }
        for (String p2 : merchantAuthService.selectPoint()) {
            list.add(p2);
        }
        return ResultMsg.success("查询经纬度成功~", list);
    }

    @ApiOperation("查询车位")
    @GetMapping(value = "/stalls", produces = "application/json;UTF-8")
    public ResultMsg stalls(@RequestParam("mobile") String mobile) {
        if (mobile != null) {
            List<PerStall> perStalls = perStallService.findStallsByUid(perUserService.findByTel(mobile).getPerUserId());
            return ResultMsg.success("查询车位成功~", perStalls);
        }
        return ResultMsg.failed("查询车位失败~");
    }

    @ApiOperation("删除车位")
    @DeleteMapping(value = "/deleteStall", produces = "application/json;UTF-8")
    public ResultMsg deleteStall(@RequestParam("perStallId") int perStallId) {
        perStallService.deleteByPrimaryKey(perStallId);
        return ResultMsg.success("删除车位成功~");
    }

    @ApiOperation("根据经纬度查询车位")
    @GetMapping(value = "/findStall", produces = "application/json;UTF-8")
    public ResultMsg findStall(@RequestParam("position") String position) {
        if (position != null) {
            PerStall perStall = perStallService.findByLnglat(position);
            return ResultMsg.success("根据经纬度查询车位成功~", perStall);
        }
        return ResultMsg.failed("根据经纬度查询车位失败");
    }
}
